package com.example.firstdemo.ui.screens.pages.addProblem.addProblemDetail

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptionsBuilder
import androidx.navigation.compose.composable
import androidx.navigation.toRoute
import kotlinx.serialization.Serializable

@Serializable
data class AddProblemDetail(
    val passTime: String,
    val uid: String,
    val stationName: String,
)

fun NavController.navigateToAddProblemDetail(
    passTime: String,
    uid: String,
    stationName: String,
    builder: NavOptionsBuilder.() -> Unit = {}
) {
    navigate(
        AddProblemDetail(
            passTime = passTime,
            uid = uid,
            stationName
        ),
        builder
    )
}


fun NavGraphBuilder.addProblemDetailDestination(
    padding: PaddingValues = PaddingValues(0.dp),
    navController: NavController,
) {
    composable<AddProblemDetail> { backStackEntry ->
        val routeData: AddProblemDetail = backStackEntry.toRoute()
        val viewModel: AddProblemDetailViewModel = viewModel()
        val uiState = viewModel.uiState.collectAsStateWithLifecycle().value

        fun getDetail() {
            viewModel.getAddProblemDetail(routeData.passTime, routeData.uid, routeData.stationName)
        }

        AddProblemDetailScreen(
            padding = padding,
            uiState = uiState,
            onBack = navController::popBackStack,
            onGetDetail = ::getDetail,
        )
    }
}